package cn.iocoder.yudao.module.project.controller.admin.kismaterialreceipt;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.iocoder.yudao.framework.common.exception.ServerException;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.project.dal.dataobject.kisbatchcode.KisBatchCodeDO;
import cn.iocoder.yudao.module.project.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.project.dal.mysql.kisbatchcode.KisBatchCodeMapper;
import cn.iocoder.yudao.module.project.dal.mysql.kismaterialreceipt.KisMaterialReceiptMapper;
import cn.iocoder.yudao.module.project.dal.mysql.supplier.SupplierMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.module.project.config.projectDefine.HTTP_STATUS.ERROR_STATUS;

import cn.iocoder.yudao.module.project.controller.admin.kismaterialreceipt.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kismaterialreceipt.KisMaterialReceiptDO;
import cn.iocoder.yudao.module.project.service.kismaterialreceipt.KisMaterialReceiptService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 物料入库单")
@RestController
@RequestMapping("/project/kis-material-receipt")
@Validated
@Slf4j
public class KisMaterialReceiptController {

    @Resource
    private KisMaterialReceiptService kisMaterialReceiptService;

    @Resource
    private KisMaterialReceiptMapper kisMaterialReceiptMapper;

    @Resource
    private SupplierMapper supplierMapper;

    @Resource
    private KisBatchCodeMapper kisBatchCodeMapper;

    @PostMapping("/create")
    @Operation(summary = "创建物料入库单")
    // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:create')")
    public CommonResult<String> createKisMaterialReceipt(@Valid @RequestBody KisMaterialReceiptSaveReqVO createReqVO) {
        return success(kisMaterialReceiptService.createKisMaterialReceipt(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料入库单")
    // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:update')")
    public CommonResult<Boolean> updateKisMaterialReceipt(@Valid @RequestBody KisMaterialReceiptSaveReqVO updateReqVO) {
        kisMaterialReceiptService.updateKisMaterialReceipt(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料入库单")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:delete')")
    public CommonResult<Boolean> deleteKisMaterialReceipt(@RequestParam("id") String id) {
        kisMaterialReceiptService.deleteKisMaterialReceipt(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料入库单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:query')")
    public CommonResult<KisMaterialReceiptRespVO> getKisMaterialReceipt(@RequestParam("id") String id) {
        KisMaterialReceiptDO kisMaterialReceipt = kisMaterialReceiptService.getKisMaterialReceipt(id);
        KisMaterialReceiptRespVO result = BeanUtils.toBean(kisMaterialReceipt, KisMaterialReceiptRespVO.class);

        // 格式化供应商
        List<SupplierDO> supplierDOS = supplierMapper.selectList();
        if (null != result) {
            for (SupplierDO supplierDO : supplierDOS) {
                String name = supplierDO.getName();
                String supplierId = supplierDO.getId();
                if (StringUtils.equals(result.getSupplier(), supplierId)) {
                    result.setSupplierName(name);
                }
            }
        }

        return success(result);
    }

    // @GetMapping("/page")
    // @Operation(summary = "获得物料入库单分页")
    // // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:query')")
    // public CommonResult<PageResult<KisMaterialReceiptRespVO>> getKisMaterialReceiptPage(@Valid KisMaterialReceiptPageReqVO pageReqVO) {
    //     PageResult<KisMaterialReceiptDO> pageResult = kisMaterialReceiptService.getKisMaterialReceiptPage(pageReqVO);
    //
    //     PageResult<KisMaterialReceiptRespVO> result = BeanUtils.toBean(pageResult, KisMaterialReceiptRespVO.class);
    //     // 格式化供应商
    //     if (CollectionUtil.isNotEmpty(result.getList())) {
    //         List<SupplierDO> supplierDOS = supplierMapper.selectList();
    //         if (CollectionUtil.isNotEmpty(supplierDOS)) {
    //             for (KisMaterialReceiptRespVO kisMaterialReceiptRespVO : result.getList()) {
    //                 for (SupplierDO supplierDO : supplierDOS) {
    //                     String name = supplierDO.getName();
    //                     String supplierId = supplierDO.getId();
    //                     if (StringUtils.equals(kisMaterialReceiptRespVO.getSupplier(), supplierId)) {
    //                         kisMaterialReceiptRespVO.setSupplierName(name);
    //                     }
    //                 }
    //             }
    //         }
    //
    //     }
    //     return success(result);
    // }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料入库单 Excel")
    // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKisMaterialReceiptExcel(@Valid KisMaterialReceiptPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KisMaterialReceiptDO> list = kisMaterialReceiptService.getKisMaterialReceiptPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料入库单.xls", "数据", KisMaterialReceiptRespVO.class,
                        BeanUtils.toBean(list, KisMaterialReceiptRespVO.class));
    }




    @Operation(summary = "导出物料入库单模板 Excel")
    @GetMapping(value = "/exportTemplate")
    public void exportTemplate(HttpServletResponse response){
        InputStream inputStream = null;
        String fileName = "入库单导入模板.xlsx";
        try {
            ClassPathResource classPathResource = new ClassPathResource("/template/入库单导入模板.xlsx");
            inputStream = classPathResource.getInputStream();
            Workbook excel = WorkbookFactory.create(inputStream);

            //使用输出流进行表格下载,基于浏览器作为客户端下载
            OutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));;//指定以附件形式进行下载
            excel.write(out);
            out.flush();
            out.close();
            excel.close();
        } catch (Exception e) {
        } finally {
            IoUtil.close(inputStream);
        }
    }

    @PostMapping("/importMaterialReceipt")
    @Operation(summary = "导入物料入库单 Excel")
    public CommonResult<Boolean> importMaterialReceipt(@RequestParam("file") MultipartFile file) {
        try {
            long start = System.currentTimeMillis();
            ExcelReader excelReader = ExcelUtil.getReader(file.getInputStream());
            Sheet sheet = excelReader.getSheet();
            List<List<Object>> list = excelReader.read();

            if (CollectionUtil.isNotEmpty(list)) {
                if (list.size() > 2) {
                    List<KisMaterialReceiptDO> importDataList = new ArrayList<>();

                    String dateStr = "";
                    String supplier = "";
                    String auditMark = "";
                    String code = "";
                    // 从第三行开始读取,一直读到倒数第二行
                    for (int i = 2; i < list.size() - 1; i++) {
                        KisMaterialReceiptDO internalCodeDO = new KisMaterialReceiptDO();
                        // 日期
                        Object dateStrObj = list.get(i).get(0);
                        if (dateStrObj != null && !dateStrObj.equals("")) {
                            dateStr = dateStrObj.toString().trim()
                                    .replace("/", "-")
                                    .replace(".", "-");
                        }
                        if (StringUtils.isNotBlank(dateStr)) {
                            DateTime parseDate = DateUtil.parse(dateStr);
                            internalCodeDO.setDateStr(parseDate.toDateStr());

                            // String[] dateArr = dateStr.split("-");
                            // if (dateArr.length == 3) {
                            //     String year = dateArr[0];
                            //     String month = dateArr[1];
                            //     String day = dateArr[2];
                            //
                            //     if (month.length() == 1) {
                            //         month = "0" + month;
                            //     }
                            //     if (day.length() == 1) {
                            //         day = "0" + day;
                            //     }
                            //     dateStr = year + "-" + month + "-" + day;
                            //     LocalDate localDate = LocalDate.parse(dateStr);
                            //     LocalDateTime receiptDateTime = localDate.atTime(0, 0, 0);
                            //     internalCodeDO.setReceiptDate(receiptDateTime);
                            // }

                            LocalDateTime receiptDateTime = parseDate.toLocalDateTime();
                            internalCodeDO.setReceiptDate(receiptDateTime);
                        }

                        // 供应商
                        Object supplierObj = list.get(i).get(1);
                        if (supplierObj != null && !supplierObj.equals("")) {
                            supplier = supplierObj.toString().trim();
                        }
                        if (StringUtils.isNotBlank(supplier)) {
                            SupplierDO supplierDO = supplierMapper.selectOne(SupplierDO::getName, supplier);
                            if (null != supplierDO) {
                                String supplierId = supplierDO.getId();
                                internalCodeDO.setSupplier(supplierId);
                            }
                            if (null == supplierDO) {
                                Assert.isNull(null, "供应商名称:" + supplier + "未入库");
                            }
                        }

                        // 审核标志
                        Object auditMarkObj = list.get(i).get(2);
                        if (auditMarkObj != null && !auditMarkObj.equals("")) {
                            auditMark = auditMarkObj.toString().trim();
                        }
                        if (StringUtils.isNotBlank(auditMark)) {
                            internalCodeDO.setAuditMark(auditMark);
                        }

                        // 编号
                        Object codeObj = list.get(i).get(3);
                        if (codeObj != null && !codeObj.equals("")) {
                            code = codeObj.toString().trim();
                        }
                        if (StringUtils.isNotBlank(code)) {
                            internalCodeDO.setCode(code);
                        }

                        // 收料仓库
                        Object warehouse = list.get(i).get(4);
                        if (warehouse != null && !warehouse.equals("")) {
                            internalCodeDO.setWarehouse(warehouse.toString());
                        }

                        // 物料长代码
                        Object materialCode = list.get(i).get(5);
                        if (materialCode != null && !materialCode.equals("")) {
                            internalCodeDO.setMaterialCode(materialCode.toString());
                        }

                        // 物料名称
                        Object materialName = list.get(i).get(6);
                        if (materialName != null && !materialName.equals("")) {
                            internalCodeDO.setMaterialName(materialName.toString());
                        }

                        // 规格型号
                        Object specification = list.get(i).get(7);
                        if (specification != null && !specification.equals("")) {
                            internalCodeDO.setSpecification(specification.toString());
                        }

                        // 应收数量
                        Object receivableNum = list.get(i).get(8);
                        if (receivableNum != null && !receivableNum.equals("")) {
                            if (NumberUtil.isNumber(receivableNum.toString())) {
                                internalCodeDO.setReceivableNum(Double.valueOf(receivableNum.toString()));
                            }
                        }

                        // 实收数量
                        Object actualNum = list.get(i).get(9);
                        if (actualNum != null && !actualNum.equals("")) {
                            if (NumberUtil.isNumber(actualNum.toString())) {
                                internalCodeDO.setActualNum(Double.valueOf(actualNum.toString()));
                            }
                        }

                        // 单位
                        Object unit = list.get(i).get(10);
                        if (unit != null && !unit.equals("")) {
                            internalCodeDO.setUnit(unit.toString());
                        }

                        // 源单单号
                        Object materialNumber = list.get(i).get(11);
                        if (materialNumber != null && !materialNumber.equals("")) {
                            internalCodeDO.setMaterialNumber(materialNumber.toString());
                        }

                        // 源单类型
                        Object materialType = list.get(i).get(12);
                        if (materialType != null && !materialType.equals("")) {
                            internalCodeDO.setMaterialType(materialType.toString());
                        }

                        // 部门
                        Object dept = list.get(i).get(13);
                        if (dept != null && !dept.equals("")) {
                            internalCodeDO.setDept(dept.toString());
                        }

                        // 业务员
                        Object operator = list.get(i).get(14);
                        if (operator != null && !operator.equals("")) {
                            internalCodeDO.setOperator(operator.toString());
                        }

                        // 批号
                        Object batchNumber = list.get(i).get(15);
                        if (batchNumber != null && !batchNumber.equals("")) {
                            internalCodeDO.setBatchNumber(batchNumber.toString());
                        }

                        // 备注
                        Object remark = list.get(i).get(16);
                        if (remark != null && !remark.equals("")) {
                            internalCodeDO.setRemark(remark.toString());
                        }

                        internalCodeDO.setReceiptCode(internalCodeDO.getDateStr() + "_" + internalCodeDO.getSupplier() + "_" + internalCodeDO.getCode());
                        internalCodeDO.setDeleted(false);
                        importDataList.add(internalCodeDO);
                    }

                    if (CollectionUtil.isNotEmpty(importDataList)) {
                        // 检查当前批次是否在数据库中存在,同一批次不能导入多次
                        List<String> codeList = importDataList.stream()
                                .map(c -> c.getCode())
                                .distinct()
                                .collect(Collectors.toList());
                        LambdaQueryWrapper<KisMaterialReceiptDO> existQueryWrapper = new LambdaQueryWrapper<>();
                        existQueryWrapper.in(KisMaterialReceiptDO::getCode, codeList);
                        List<KisMaterialReceiptDO> existKisMaterialReceiptDOS = kisMaterialReceiptMapper.selectList(existQueryWrapper);
                        if (CollectionUtil.isNotEmpty(existKisMaterialReceiptDOS)) {
                            throw new ServiceException(ERROR_STATUS, "当前批次已存在,请勿重复导入当前批次");
                        }

                        // kisMaterialReceiptService.insertRecord(importDataList);
                        System.out.println("取excel完毕：" +(System.currentTimeMillis() - start)+"}毫秒");

                        for (KisMaterialReceiptDO internalCodeDO : importDataList) {
                            String receiptDateStr = internalCodeDO.getDateStr();
                            String materialCode = internalCodeDO.getMaterialCode();
                            String supplierId = internalCodeDO.getSupplier();
                            String batchCode = internalCodeDO.getCode();
                            internalCodeDO.setId(receiptDateStr + "_" + materialCode + "_" + supplierId + "_" + batchCode);
                            LambdaQueryWrapper<KisMaterialReceiptDO> queryWrapper = new LambdaQueryWrapper<>();
                            queryWrapper.eq(KisMaterialReceiptDO::getId, internalCodeDO.getId());
                            // queryWrapper.eq(KisMaterialReceiptDO::getDateStr, receiptDateStr);
                            // queryWrapper.eq(KisMaterialReceiptDO::getMaterialCode, materialCode);
                            // queryWrapper.eq(KisMaterialReceiptDO::getSupplier, supplierId);
                            // queryWrapper.eq(KisMaterialReceiptDO::getCode, batchCode);
                            KisMaterialReceiptDO kisMaterialReceiptDO = kisMaterialReceiptMapper.selectOne(queryWrapper);
                            if (null == kisMaterialReceiptDO) {
                                kisMaterialReceiptMapper.insert(internalCodeDO);
                            }

                        }

                        // 物料入库后,同时生成箱码,箱码的规则是物料编号+供应商编码+批次号
                        List<KisMaterialReceiptDO> materialList = importDataList.stream()
                                .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(u -> u.getDateStr() + ";" + u.getMaterialCode() + ";" + u.getSupplier() + ";" + u.getBatchNumber() + ";" + u.getReceiptCode()))), ArrayList::new));
                        if (CollectionUtil.isNotEmpty(materialList)) {
                            // List<KisBatchCodeDO> kisBatchCodeDOS = new ArrayList<>();
                            for (KisMaterialReceiptDO kisMaterialReceiptDO : materialList) {
                                String receiptDateStr = kisMaterialReceiptDO.getDateStr();
                                String supplierId = kisMaterialReceiptDO.getSupplier();
                                String batchCode = kisMaterialReceiptDO.getCode();
                                String materialCode = kisMaterialReceiptDO.getMaterialCode();

                                KisBatchCodeDO kisBatchCodeDO = new KisBatchCodeDO();
                                kisBatchCodeDO.setDateStr(receiptDateStr);
                                kisBatchCodeDO.setBatchCode(batchCode);
                                kisBatchCodeDO.setMaterialCode(materialCode);

                                SupplierDO supplierDO = supplierMapper.selectOne(SupplierDO::getId, supplierId);
                                if (null != supplierDO) {
                                    String existssupplierId = supplierDO.getId();
                                    kisBatchCodeDO.setSupplierId(existssupplierId);

                                    kisBatchCodeDO.setId(materialCode + "_" + supplierId + "_" + batchCode);
                                    LambdaQueryWrapper<KisBatchCodeDO> queryWrapper = new LambdaQueryWrapper<>();
                                    queryWrapper.eq(KisBatchCodeDO::getId, kisBatchCodeDO.getId());
                                    // queryWrapper.eq(KisBatchCodeDO::getDateStr, receiptDateStr);
                                    // queryWrapper.eq(KisBatchCodeDO::getMaterialCode, materialCode);
                                    // queryWrapper.eq(KisBatchCodeDO::getSupplier, supplierId);
                                    // queryWrapper.eq(KisBatchCodeDO::getCode, batchCode);
                                    KisBatchCodeDO exsitsBatchCode = kisBatchCodeMapper.selectOne(queryWrapper);
                                    if (null == exsitsBatchCode) {
                                        kisBatchCodeMapper.insert(kisBatchCodeDO);
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }
        catch (IllegalArgumentException iae) {
            log.error(iae.getMessage());
            throw new ServiceException(ERROR_STATUS, iae.getMessage());
        }
        catch (ServiceException se) {
            log.error(se.getMessage());
            throw new ServiceException(ERROR_STATUS, se.getMessage());
        }
        catch (Exception e) {
            log.error(e.getMessage());
            throw new ServiceException(ERROR_STATUS, "导入物料入库单失败");
        }
        return success(true);
    }

    @GetMapping("/receiptReport")
    @Operation(summary = "根据条件查询入库报表")
    public CommonResult<List<MaterialReceiptReportVO>> selectMaterialReceiptReportByDate(MaterialReceiptReportBO materialReceiptReportBO) {
        List<MaterialReceiptReportVO> materialReceiptReportVOS = kisMaterialReceiptService.selectMaterialReceiptReportByDate(materialReceiptReportBO);
        return success(materialReceiptReportVOS);
    }

    @RequestMapping("/receiptPage")
    @Operation(summary = "获得物料入库单分页")
    // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:query')")
    public CommonResult<PageResult<KisMaterialReceiptListVO>> getReceiptPage(@Valid @RequestBody KisMaterialReceiptPageReqVO pageReqVO) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", (pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize());
        params.put("pageSize", pageReqVO.getPageSize());

        String materialCode = pageReqVO.getMaterialCode();
        if (StringUtils.isNotBlank(materialCode)) {
            params.put("materialCode", materialCode);
        }

        String supplierId = pageReqVO.getSupplierId();
        if (StringUtils.isNotBlank(supplierId)) {
            params.put("supplierId", supplierId);
        }

        String batchCode = pageReqVO.getBatchCode();
        if (StringUtils.isNotBlank(batchCode)) {
            params.put("batchCode", batchCode);
        }

        String[] dateReq = pageReqVO.getDateReq();
        if (null != dateReq && dateReq.length == 2) {
            String startTime = dateReq[0];
            String endTime = dateReq[1];
            if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
                LocalDateTime startDate = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                LocalDateTime endDate = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                params.put("startDate", startDate);
                params.put("endDate", endDate);
            }
        }

        List<KisMaterialReceiptListVO> kisMaterialReceiptListVOS = kisMaterialReceiptService.selectMaterialReceiptList(params);
        Long count = kisMaterialReceiptService.countMaterialReceiptList(params);
        PageResult<KisMaterialReceiptListVO> result = new PageResult<>();
        result.setList(kisMaterialReceiptListVOS);
        result.setTotal(count);

        // 格式化供应商
        if (CollectionUtil.isNotEmpty(result.getList())) {
            List<SupplierDO> supplierDOS = supplierMapper.selectList();
            if (CollectionUtil.isNotEmpty(supplierDOS)) {
                for (KisMaterialReceiptListVO kisMaterialReceiptRespVO : result.getList()) {
                    for (SupplierDO supplierDO : supplierDOS) {
                        String name = supplierDO.getName();
                        if (StringUtils.equals(kisMaterialReceiptRespVO.getSupplierId(), supplierDO.getId())) {
                            kisMaterialReceiptRespVO.setSupplierName(name);
                        }
                    }
                }
            }

        }
        return success(result);
    }

    @GetMapping("/receiptDetail")
    @Operation(summary = "根据入库单获得物料入库单详情")
    // @PreAuthorize("@ss.hasPermission('project:kis-material-receipt:query')")
    public CommonResult<PageResult<KisMaterialReceiptRespVO>> getReceiptDetail(@Valid KisMaterialReceiptPageReqVO pageReqVO) {
        String receiptCode = pageReqVO.getReceiptCode();
        if (StringUtils.isBlank(receiptCode)) {
            throw new ServiceException(ERROR_STATUS, "物料入库单不能为空");
        }

        List<KisMaterialReceiptDO> kisMaterialReceiptDOS = kisMaterialReceiptMapper.selectList("receipt_code", receiptCode);
        List<KisMaterialReceiptRespVO> kisMaterialReceiptRespVOS = BeanUtils.toBean(kisMaterialReceiptDOS, KisMaterialReceiptRespVO.class);

        // 格式化供应商
        if (CollectionUtil.isNotEmpty(kisMaterialReceiptRespVOS)) {
            List<SupplierDO> supplierDOS = supplierMapper.selectList();
            if (CollectionUtil.isNotEmpty(supplierDOS)) {
                for (KisMaterialReceiptRespVO kisMaterialReceiptRespVO : kisMaterialReceiptRespVOS) {
                    for (SupplierDO supplierDO : supplierDOS) {
                        String name = supplierDO.getName();
                        if (StringUtils.equals(kisMaterialReceiptRespVO.getSupplier(), supplierDO.getId())) {
                            kisMaterialReceiptRespVO.setSupplierName(name);
                        }
                    }
                }
            }

        }

        int size = kisMaterialReceiptRespVOS.size();
        PageResult<KisMaterialReceiptRespVO> result = new PageResult<>();

        Integer pageNo = 1;
        Integer pageSize = 10;
        if (null != pageReqVO.getPageNo()) {
            pageNo = pageReqVO.getPageNo();
        }
        if (null != pageReqVO.getPageSize()) {
            pageSize = pageReqVO.getPageSize();
        }
        result.setTotal((long) size);
        if (CollectionUtil.isNotEmpty(kisMaterialReceiptRespVOS)) {
            List<KisMaterialReceiptRespVO> collect = kisMaterialReceiptRespVOS.stream()
                    .skip((pageNo - 1) * pageSize) // 跳过前面的元素
                    .limit(pageSize) // 限制输出的元素数量
                    .collect(Collectors.toList());
            result.setList(collect);
        }
        return success(result);
    }

}